package cn.chainco.cts.idaoImpl;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.chainco.cts.idao.IDataDictionaryDao;
import cn.chainco.cts.jdo.Company;
import cn.chainco.cts.jdo.DataDictionary;
import cn.chainco.cts.jdo.Work;

public class DataDictionaryDaoImpl extends GenericHibernateDaoImpl<DataDictionary, Long> implements IDataDictionaryDao{

	@SuppressWarnings("unchecked")
	public DataDictionary findDataById(int id) {
		String sql = "select * from cts_datadictionary where 1=1 and id="+id;
		List<DataDictionary> list = this.getSession().createSQLQuery(sql).addEntity(DataDictionary.class).list();
		if(list.size()>0){
			return list.get(0);
		}
		return null;
	}

	/**
	 * 
	 * @param type 可为null
	 * @param typeDetail 可为null
	 * @param status 是否可用(可为null)
	 * @param orderNo 是否可用(可为null)1.asc , 2.desc
	 * @return list
	 */
	@SuppressWarnings("unchecked")
	public List<DataDictionary> findDataByType(Integer type, Integer typeDetail, Integer status, Integer orderNo) {
		String sql = "select * from cts_datadictionary where 1=1";
		if(type !=null){
			sql += " and type="+type;
		}
		
		if(typeDetail !=null){
			sql += " and typeDetail="+typeDetail;
		}
		
		if(status !=null){
			sql += " and status="+status;
		}
		
		if(orderNo !=null && orderNo == 1){
			sql += " order by orderNo asc";
		}else{
			sql += " order by orderNo desc";
		}
		
		List<DataDictionary> list = this.getSession().createSQLQuery(sql).addEntity(DataDictionary.class).list();
		return list;
	}

	/**
	 * @param name 名称
	 * @param type 可为null
	 * @param typeDetail 可为null
	 * @param status 是否可用(可为null)
	 * @param orderNo 是否可用(可为null)1.asc , 2.desc
	 * @return
	 */
	public List<DataDictionary> findDataByType(String name , String type, String typeDetail, String status, String orderNo){
		String sql = "select * from cts_datadictionary where 1=1";
		
		if(name !=null && !name.trim().equals("")){
			sql += " and name_zh like '%"+name+"%' or name_en like '%"+name+"%'";
		}
		
		if(type !=null && !type.trim().equals("")){
			sql += " and type="+type;
		}
		
		if(typeDetail !=null && !typeDetail.trim().equals("")){
			sql += " and typeDetail="+typeDetail;
		}
		
		if(status !=null && !status.trim().equals("")){
			sql += " and status="+status;
		}
		
		if(orderNo !=null && !orderNo.trim().equals("")){
			if(orderNo.equals("1")){
				sql += " order by orderNo asc";
			}else if(orderNo.equals("2")){
				sql += " order by orderNo desc";
			}
		}
		
		
		List<DataDictionary> list = this.getSession().createSQLQuery(sql).addEntity(DataDictionary.class).list();
		return list;
	}
	/**
	 * 数据字典
	 */
	@SuppressWarnings("unchecked")
	public List<DataDictionary> findDictionaryByCompany(Company company , String type , String typeDetail , Integer status){
		String sql = " select * from cts_datadictionary where 1=1 and type=" + type + " and typeDetail=" + typeDetail;
		if(status!=null){
			sql += " and status="+status;
		}else{
			sql += " and status=0";
		}
		
		List<DataDictionary> itemlist = this.getSession().createSQLQuery(sql).addEntity(DataDictionary.class).list();
		return itemlist;
	}
	
	/**
	 * 查找数据字典是否被订单引用
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Work> findWorkByDicId(Long id){
		String sql = "select * from cts_work where 1=1 and ";
		sql += "(";
		sql += " cargoTypeId="+id;
		sql += " or cy2cyId1="+id;
		sql += " or paymentId1="+id;
		sql += " or releaseCargoTypeId1="+id;
		sql += " or cy2cyId2="+id;
		sql += " or paymentId2="+id;
		sql += " or releaseCargoTypeId2="+id;
		sql += " or packageTypeId="+id;
		sql += " or transhipTypeId1="+id;
		sql += " or transhipTypeId2="+id;
		sql += " or carrierId="+id;
		sql += " or expressPackageTypeId="+id;
		sql += " or expressCarrierId="+id;
	    sql += ")";
	    
	    List<Work> worklist = this.getSession().createSQLQuery(sql).addEntity(Work.class).list();
	    return worklist;
		
	}
	
	
}
